/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.service;

import br.com.foxline.model.ActCep;
import br.com.foxline.model.NatureWriting;
import br.com.foxline.util.Msg;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;

/**
 *
 * @author roniere
 */
@Stateless
@LocalBean
public class NatureWritingService extends GenericService<NatureWriting> {

    public NatureWritingService() {
        super(NatureWriting.class);
    }

    public void saveNatureWriting(NatureWriting nature) {
        try {
            save(nature);
            Msg.messagemInfo("Natureza Escritura Salvo com Sucesso ");
        } catch (Exception erro) {
        }
    }

    public void updateNatureWriting(NatureWriting nature) {
        try {
            update(nature);
            Msg.messagemInfo("Natureza Escritura Atualizado com Sucesso ");
        } catch (Exception erro) {
        }
    }

    public List<NatureWriting> autoComplete(String str) {
        String sql = "Select nw from NatureWriting nw where lower(nw.description) like lower(:desc)";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("desc", str + "%");
        return (List<NatureWriting>) query.getResultList();
    }

    public List<NatureWriting> findNature(NatureWriting nature) {
        String sql = "Select n from NatureWriting n Where ";

        if ((!nature.getCod().equals("")) && (nature.getCod() != null)) {
            sql += "  lower(n.cod) like lower(:cod)  and ";
        }

        if ((!nature.getDescription().equals("")) && (nature.getDescription() != null)) {
            sql += "  lower(n.description) like lower(:des)  and ";
        }

        sql += " 1=1 ";

        Query query = getEntityManager().createQuery(sql);

        if ((!nature.getCod().equals("")) && (nature.getCod() != null)) {
            query.setParameter("cod", "%" + nature.getCod() + "%");
        }
        if ((!nature.getDescription().equals("")) && (nature.getDescription() != null)) {
            query.setParameter("des", "%" + nature.getDescription() + "%");
        }
        return query.getResultList();

    }

    public NatureWriting findNatureCod(String Cod) {
        NatureWriting nature; 
        String sql = "Select n from NatureWriting n Where  n.cod like lower(:cod) ";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("cod", Cod);
        try {
            nature = (NatureWriting) query.getSingleResult();
        } catch (NoResultException nr) {
            return null;
        } catch (NonUniqueResultException nre) {
            return null;
        }
        return nature;
    }
}
